// 听牌
declare var _: any
class TingPoker extends eui.Component{
    private btn:eui.Image;
    private pokerBg:eui.Image;
    private tuoguan:boolean = false;
    private pokerContentBg:egret.Sprite;
    private bgY:number;
    private cardY:number;

    public constructor(){
        super();
        this.once(eui.UIEvent.COMPLETE,this.onComplete,this);
        this.skinName = "src/Game/gameRoom/tingPokerSkin.exml";
    }
    private onComplete(){
        this.pokerContentBg = new egret.Sprite();
        this.pokerContentBg.x = 155;
        this.pokerContentBg.y = 24;

        this.bgY = this.pokerBg.y;
        this.cardY = this.pokerContentBg.y;

        this.addChild(this.pokerContentBg);
        this.pokerContentBg.touchChildren = false;
        this.pokerBg.touchEnabled = false;
        this.touchEnabled = false;
        // 隐藏托管
        this.btn.visible = false;
        this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this);
        this.once(egret.Event.REMOVED_FROM_STAGE,this.onRemove,this);
        this.setTuoGuang(this.tuoguan);
    }
    private onRemove(){
        this.btn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this);
    }
    private onClick(e:egret.TouchEvent){
        if(this.tuoguan){
            this.dispatchEventWith(PokerEvent.TUOGUAN_TINGPOKER_END);
            //alert("请求关闭托管");
            //this.tuoguan = false;
            StageData.soundObj.soundSinglePlay("isClick");
        }else{
            //this.tuoguan = true;
            this.dispatchEventWith(PokerEvent.TUOGUAN_TINGPOKER_START);
            //alert("请求开启托管");
            StageData.soundObj.soundSinglePlay("click");
        }
        this.setTuoGuang(this.tuoguan);
    }
    //设置托管
    public setTuoGuang(value:boolean){
        this.tuoguan = value;
        if(value){
            this.btn.source = "tuoguan_valid_png";
        }else{
            this.btn.source = "tuoguan_null_png";
        }
    }

    private lastTingPoker = []
    //设置听的牌
    public setTingPoker(pokerDataList:Array<any>){
        console.log('setTingPoker', pokerDataList)
        if(pokerDataList.length<1) return;
        if (_.isEqual(this.lastTingPoker, pokerDataList)) {
            return 
        } else {
            this.lastTingPoker = pokerDataList
        }
        // for(var i=this.pokerContentBg.numChildren;i>0;i--){
        //     this.pokerContentBg.removeChildAt(i-1);
        // }
        this.pokerContentBg.removeChildren()
        for(var i=0;i<pokerDataList.length;i++){
            var lsPoker:outPoker1 = new outPoker1();
            console.log('tingpaicontent', "X"+pokerDataList[i].suitType+"_"+pokerDataList[i].count+"_png")
            lsPoker.setContentOld("X"+pokerDataList[i].suitType+"_"+pokerDataList[i].count+"_png");
            lsPoker.x = (lsPoker.width+5)*i;
            console.log('lsPoker', lsPoker)
            this.pokerContentBg.addChild(lsPoker);
        }
        var lswidth:number = (lsPoker.width+5)*i;
        if(lswidth>this.pokerBg.x){
           this.pokerBg.width = lswidth+120;
        }else{
           this.pokerBg.width = 190;
        }

        if(this.pokerBg.x+this.pokerBg.width>StageData.W - 20){
            let thisW:number = this.pokerContentBg.x + (this.pokerContentBg.getChildAt(0).width*this.pokerContentBg.numChildren)+130;
            let lsScale:number = (StageData.W/((thisW+130)+20));
            
            this.pokerBg.scaleX = this.pokerBg.scaleY = lsScale;
            this.pokerContentBg.scaleX = this.pokerContentBg.scaleY = lsScale;
            this.pokerBg.y = this.bgY+(this.bgY*(1-lsScale))+5;
            this.pokerContentBg.y = this.cardY+(this.bgY*(1-lsScale))-5;
            this.pokerContentBg.x = 130; 
        }else{
            this.pokerBg.scaleX = this.pokerBg.scaleY = 1;
            this.pokerContentBg.scaleX = this.pokerContentBg.scaleY = 1;
            this.pokerBg.y = this.bgY;
            this.pokerContentBg.y = this.cardY-20;
            this.pokerContentBg.x = 155; 
        }
    }
}